package com.hqj.service;

import com.hqj.dao.AgreementDao;
import com.hqj.entity.Agreement;
import com.mysql.cj.conf.PropertyDefinitions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ZJServiceI {

    @Autowired
    private AgreementDao ad;

    public List<Map<String,Object>> find(){
        List<Map<String,Object>> list = new ArrayList<>();
        List<Agreement> ags = ad.find(null);//获取合同租赁信息
        /*
        遍历每一条合同信息，获取出开始时间与结束时间，计算每一个月的租金及显示状态(在租，退租，续租等)，租金缴纳状态
         */
        for(Agreement ag:ags){
            Date start = ag.getCreateDate();//开始时间
            Date end = ag.getEnDate();//结束时间
            //转化为Calendar，获取月份数量
            Calendar c = Calendar.getInstance();
            int nyear = c.get(Calendar.YEAR);
            int nmon = c.get(Calendar.MONTH)+1;
            Calendar c1 = Calendar.getInstance();

            c1.setTime(start);
            int syear = c1.get(Calendar.YEAR);//获取开始的年份
            int smon = c1.get(Calendar.MONTH)+1;//获取开始的月份
            Calendar c2 = Calendar.getInstance();
            c2.setTime(end);
            int eyear = c2.get(Calendar.YEAR);
            int emon = c2.get(Calendar.MONTH)+1;
            while(c1.getTimeInMillis()<=c2.getTimeInMillis()){
                Map<String,Object> map = new LinkedHashMap<>();
                map.put("homes",ag.getHome());//房源  小区名称+楼栋名称+房号
                map.put("getUser",ag.getGetUserName()+"("+ag.getGetUserIdcard()+")");//出租方   名称(身份证号)
                map.put("setUser",ag.getSetUserName()+"("+ag.getSetUserIdcard()+")");//承租方   名称(身份证号)
                map.put("time",syear+"-"+smon);//月份
                map.put("money",ag.getGetMoney()+"/"+(ag.getGetDw().equals("year")?"年":(ag.getGetDw().equals("month")?"月":"天")));
                String status = "未支付";
                String statusNo = "1";
                if(syear==nyear&&smon == nmon){
                    status="<font color=red>"+status+"(当前月份)</font>";
                    statusNo = "1";
                }else if(c1.getTimeInMillis()>c.getTimeInMillis()){
                    status="<font color=gray>未出账</font>";
                    statusNo = "2";
                }else if(c1.getTimeInMillis()<c.getTimeInMillis()){
                    status = "<font color=green>已支付</font>";
                    statusNo = "3";
                }
                map.put("status",status);
                map.put("statusNo",statusNo);
                list.add(map);
                c1.add(Calendar.MONTH,1);
                smon++;
                if(smon>12){
                    syear++;
                    ag.setGetMoney(ag.getGetMoney()+ag.getGetMoney()*ag.getZfb());
                    smon = 1;
                }
            }

        }

        return list;
    }

}
